﻿using AnesSystem.Models;
using AnesSystem.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    public class OperationDetailsViewModel : BaseViewModel<OperationDetailsViewModel>
    {
        #region Variables 
        private ObservableCollection<NoTypeAggregateModel> _noTypeAggregateModelList = new ObservableCollection<NoTypeAggregateModel>();

        private List<NoTypeAggregateModel> _typeAggregateModelList = new List<NoTypeAggregateModel>();

        private string _useTime = "";


        #endregion


        #region Constructor
        public OperationDetailsViewModel()
        {
            Load_Page();
            Updata("");
        }
        #endregion


        #region Properties
        public ObservableCollection<NoTypeAggregateModel> NoTypeAggregateModelList
        {
            get
            {
                return _noTypeAggregateModelList;
            }

            set
            {
                _noTypeAggregateModelList = value;
                RaisePropertyChanged("NoTypeAggregateModelList");
            }
        }

        public List<NoTypeAggregateModel> TypeAggregateModelList
        {
            get
            {
                return _typeAggregateModelList;
            }

            set
            {
                _typeAggregateModelList = value;
                RaisePropertyChanged("NoTypeAggregateModelList");
            }
        }







        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods


        #endregion


        #region Public/ProtectedMethods
        public void UpdateEndTime(NoTypeAggregateModel _noTypeAggregateModel, bool sel)
        {
            _noTypeAggregateModel.SelEndCheckBox = sel;
            if (_noTypeAggregateModel.SelEndCheckBox)
            {
                _noTypeAggregateModel.IsShowEndTime = "Visible";
                if (_noTypeAggregateModel.PatientDrugsModel != null)
                {
                    _noTypeAggregateModel.PatientDrugsModel.EndTime = DateTime.Now;
                    _noTypeAggregateModel.PatientDrugsModel.Status = Constants.ACTION_FINISH;
                    PatientDrugsService.Instance.addUpdate(_noTypeAggregateModel.PatientDrugsModel);
                }

                else
                {
                    _noTypeAggregateModel.PatientEventsModel.EndTime = DateTime.Now;
                    PatientEventsService.Instance.Update(_noTypeAggregateModel.PatientEventsModel);
                }
            }
            else
            {
                _noTypeAggregateModel.IsShowEndTime = "Hidden";

                if (_noTypeAggregateModel.PatientDrugsModel != null)
                {
                    _noTypeAggregateModel.PatientDrugsModel.EndTime = new DateTime();
                    _noTypeAggregateModel.PatientDrugsModel.Status = Constants.ACTION_WORK;
                    PatientDrugsService.Instance.addUpdate(_noTypeAggregateModel.PatientDrugsModel);
                }
                else
                {
                    _noTypeAggregateModel.PatientEventsModel.EndTime = new DateTime();
                    PatientEventsService.Instance.Update(_noTypeAggregateModel.PatientEventsModel);
                }
            }

            RaisePropertyChanged("NoTypeAggregateModelList");
        }

        public void ChangeTakeOut(NoTypeAggregateModel noTypeAggregateModel, bool isChecked)
        {
            if (noTypeAggregateModel.PatientDrugsModel == null)
            {
                return;
            }
            else
            {
                noTypeAggregateModel.PatientDrugsModel.TakeOut = isChecked;
                OperationDataPool.Instance.CurOperationData.DrugsDataCache.AddAndUpdateExe(noTypeAggregateModel.PatientDrugsModel);
            }
        }


        //根据类型（type）和是否单次、输血输液（noType）改变
        public void Refresh(string updateString)
        {
            switch (updateString)
            {
                case "qb": Updata(""); break;
                case "dcyy": Updata("sp"); break;
                case "sj": Updata("事"); break;
                case "yy": Updata("用"); break;
                case "cxyy": Updata("shuzhong"); break;
                case "sxsy": Updata("shuye"); break;

            }
        }

        private void Updata(string updateString)
        {
            _noTypeAggregateModelList.Clear();
            IEnumerable<NoTypeAggregateModel> tempList = _typeAggregateModelList.Where(O =>
             O.NoType.Contains(updateString) || O.ItemType.Contains(updateString)).ToList();
            foreach (NoTypeAggregateModel not in tempList)
            {
                _noTypeAggregateModelList.Add(not);

            }
            RaisePropertyChanged("NoTypeAggregateModelList");
        }


        public void Load_Page()
        {
            NoTypeAggregateModelList.Clear();
            TypeAggregateModelList.Clear();
            List<PatientDrugsModel> drugList = OperationDataPool.Instance.CurOperationData.DrugsDataCache.AllPatientDrugsList.ToList();
            foreach (PatientDrugsModel item in drugList)
            {
                NoTypeAggregateModel noTypeAggregateModelTemp = new NoTypeAggregateModel();
                noTypeAggregateModelTemp.ItemType = "用药";
                noTypeAggregateModelTemp.NoType = item.Type;
                if (noTypeAggregateModelTemp.NoType.Equals("special"))
                {
                    noTypeAggregateModelTemp.IsShowEnd = "Hidden";
                    noTypeAggregateModelTemp.IsShowEndTime = "Hidden";
                    noTypeAggregateModelTemp.StartDataTime = item.BeginTime;
                    noTypeAggregateModelTemp.PatientDrugsModel = item;
                    _typeAggregateModelList.Add(noTypeAggregateModelTemp);
                }
                else if (noTypeAggregateModelTemp.NoType.Equals("shuzhong"))
                {
                    noTypeAggregateModelTemp.IsShowEnd = "Visible";
                    noTypeAggregateModelTemp.IsShowEndTime = "Visible";
                    if (!item.EndTime.Equals(new DateTime()))
                    {
                        noTypeAggregateModelTemp.SelEndCheckBox = true;
                        //使用时长
                        noTypeAggregateModelTemp.UseTime = (item.EndTime - item.BeginTime).Days + "天"
                            + (item.EndTime - item.BeginTime).Hours + "时" + (item.EndTime - item.BeginTime).Minutes + "分";
                    }
                    else
                    {
                        noTypeAggregateModelTemp.IsShowEndTime = "Hidden";
                        noTypeAggregateModelTemp.SelEndCheckBox = false;
                        noTypeAggregateModelTemp.UseTime = (DateTime.Now - item.BeginTime).Days + "天"
                            + (DateTime.Now - item.BeginTime).Hours + "时" + (DateTime.Now - item.BeginTime).Minutes + "分";
                    }
                    noTypeAggregateModelTemp.StartDataTime = item.BeginTime;
                    noTypeAggregateModelTemp.PatientDrugsModel = item;
                    _typeAggregateModelList.Add(noTypeAggregateModelTemp);

                }
                else if (noTypeAggregateModelTemp.NoType.Equals("shuye"))
                {
                    noTypeAggregateModelTemp.IsShowEnd = "Visible";
                    //显示结束时间
                    noTypeAggregateModelTemp.IsShowEndTime = "Visible";
                    //勾选显示结束时间
                    if (item.EndTime.Year > 2008)
                    {
                        noTypeAggregateModelTemp.SelEndCheckBox = true;
                        //使用时长
                        noTypeAggregateModelTemp.UseTime = (item.EndTime - item.BeginTime).Days + "天"
                            + (item.EndTime - item.BeginTime).Hours + "时" + (item.EndTime - item.BeginTime).Minutes + "分";
                    }
                    else
                    {
                        noTypeAggregateModelTemp.IsShowEndTime = "Hidden";
                        noTypeAggregateModelTemp.SelEndCheckBox = false;
                        noTypeAggregateModelTemp.UseTime = (DateTime.Now - item.BeginTime).Days + "天"
                            + (DateTime.Now - item.BeginTime).Hours + "时" + (DateTime.Now - item.BeginTime).Minutes + "分";
                    }
                    noTypeAggregateModelTemp.StartDataTime = item.BeginTime;
                    noTypeAggregateModelTemp.PatientDrugsModel = item;
                    _typeAggregateModelList.Add(noTypeAggregateModelTemp);

                }
            }

            List<PatientEventsModel> aventList = PatientEventsService.Instance.GetPatientEvents8Id(DataManager.Instance.CurrentPatientLink.Id.ToString());
            foreach (PatientEventsModel item in aventList)
            {
                ConfigEventsModel cem = null;
                if (DataManager.Instance.Code2events.ContainsKey(item.Code))
                {
                    cem = DataManager.Instance.Code2events[item.Code];
                }
                if (cem == null)
                {
                    continue;
                }
                NoTypeAggregateModel noTypeAggregateModelTemp = new NoTypeAggregateModel();
                noTypeAggregateModelTemp.ItemType = "事件";
                noTypeAggregateModelTemp.PatientEventsModel = item;
                noTypeAggregateModelTemp.IsShowTakeAway = "Hidden";
                if (cem.Single)
                {
                    noTypeAggregateModelTemp.StartDataTime = item.BeginTime;
                    noTypeAggregateModelTemp.IsShowEnd = "Hidden";
                    noTypeAggregateModelTemp.IsShowTakeAway = "Hidden";
                    noTypeAggregateModelTemp.IsShowEndTime = "Hidden";
                }
                else
                {
                    noTypeAggregateModelTemp.StartDataTime = item.BeginTime;
                    if (item.EndTime.Equals(new DateTime()))
                    {
                        noTypeAggregateModelTemp.IsShowEndTime = "Hidden";
                        noTypeAggregateModelTemp.SelEndCheckBox = false;
                        noTypeAggregateModelTemp.UseTime = (DateTime.Now - item.BeginTime).Days + "天"
                            + (DateTime.Now - item.BeginTime).Hours + "时" + (DateTime.Now - item.BeginTime).Minutes + "分";
                    }
                    else
                    {
                        noTypeAggregateModelTemp.IsShowEndTime = "Visible";
                        noTypeAggregateModelTemp.SelEndCheckBox = true;
                        noTypeAggregateModelTemp.UseTime = (item.EndTime - item.BeginTime).Days + "天"
                            + (item.EndTime - item.BeginTime).Hours + "时" + (item.EndTime - item.BeginTime).Minutes + "分";
                    }
                }
                _typeAggregateModelList.Add(noTypeAggregateModelTemp);
            }
            _typeAggregateModelList.Sort((NoTypeAggregateModel a, NoTypeAggregateModel b) =>
            a.StartDataTime.CompareTo(b.StartDataTime));
        }

        public override void Refresh()
        {
            Load_Page();
            Updata("");
        }

        #endregion


        #region EventHandlers


        #endregion
    }
}
